package com.example.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 半生瓜 on 2017/2/27.
 */

public class MainActivity extends FragmentActivity implements View.OnClickListener {
//    private TextView cloud;
//    private TextView head;
//    private TextView znz;

    private ImageView imageView_hear;
    private android.support.v4.app.FragmentManager fragmentManager;
    private TextView cloud;
    private TextView head;
    private TextView znz;
   private ViewPager mViewPager;
    private TabFragmentPagerAdapter mAdpter;
    private List<Fragment> mfragmentList;
    private Button shouye;
    private Button tuichu;
    private ListView list;
    private Toolbar toolbar;
    private static final String[] strs = new String[] {
        "first", "second", "third", "fourth", "fifth"
    };



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    initWindow();

        cloud.setOnClickListener(this);
        head.setOnClickListener(this);
        znz.setOnClickListener(this);
        shouye.setOnClickListener(this);
        tuichu.setOnClickListener(this);
        imageView_hear.setOnClickListener(this);
//        defaultClick();
    }

    public void init(){
        fragmentManager=getSupportFragmentManager();
        toolbar=(Toolbar)findViewById(R.id.toolbar);
        list=(ListView)findViewById(R.id.Sliding_list) ;
        mViewPager=(ViewPager) findViewById(R.id.fl);
        cloud=(TextView)findViewById(R.id.cloud);
        head=(TextView)findViewById(R.id.head);
        znz=(TextView)findViewById(R.id.znz);
        imageView_hear=(ImageView)findViewById(R.id.image_hear);
        shouye=(Button)findViewById(R.id.shouye);
        tuichu=(Button)findViewById(R.id.tuichu);
        mfragmentList=new ArrayList<Fragment>();
       ListFragment tab1=new CloudFragment();
        Fragment tab2=new HeadFragment();
        Fragment tab3=new ZnzFragment();
        mfragmentList.add(tab1);
        mfragmentList.add(tab2);
        mfragmentList.add(tab3);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar) ;
        toolbar.setTitle("EasyLife");
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

//        NavigationView navigationView = (NavigationView) findViewById(R.id.asd);
//        navigationView.setNavigationItemSelectedListener(this);

//        ArrayList<String> list = new ArrayList<String>();
//        list.add("LinearLayout");
//       list.add("AbsoluteLayout");
//        list.add("TableLayout");
//        list.add("RelativeLayout");
//        list.add("FrameLayout");
        //fragment 的滑动操作
        mAdpter =new TabFragmentPagerAdapter(getSupportFragmentManager(),mfragmentList);
        mViewPager.setAdapter(mAdpter);
        mViewPager.setCurrentItem(1);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        cloud.setTextColor(Color.WHITE);
                        head.setTextColor(Color.GRAY);
                        znz.setTextColor(Color.GRAY);
                        break;
                    case 1:
                        cloud.setTextColor(Color.GRAY);
                        head.setTextColor(Color.WHITE);
                        znz.setTextColor(Color.GRAY);
                        break;
                    case 2:
                        cloud.setTextColor(Color.GRAY);
                        znz.setTextColor(Color.WHITE);
                        head.setTextColor(Color.GRAY);
                        break;


                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
            //沉浸式状态栏设置：变为半透明状态栏
//         Window window = getWindow();
//         window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//         window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            //策划栏 监听  参考网址http://blog.csdn.net/u011771755/article/details/45330835
//        list.setAdapter(new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1, strs));
//        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                switch (i){
//                    case 0:
//                        Toast.makeText(MainActivity.this,"ok", Toast.LENGTH_SHORT).show();break;
//                    case 1:
//                        Toast.makeText(MainActivity.this,"ok2", Toast.LENGTH_SHORT).show();break;
//                    case 2:
//                        Toast.makeText(MainActivity.this,"o3", Toast.LENGTH_SHORT).show();break;
//                }
//            }
//        });


            void initWindow() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                }
            }

            @Override

            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.cloud:
                        selectFragment(0);
                        break;
                    case R.id.head:
                        selectFragment(1);
                        break;
                    case R.id.znz:
                        selectFragment(2);
                        break;
                    default:
                        break;
                    case R.id.shouye:
//            Intent intent =new Intent(this,MainActivity.class);
//            startActivity(intent);
                        finish();
                        break;
                    case R.id.tuichu:
                        finish();
                        break;
                    case R.id.image_hear:
                        Intent intent1 = new Intent(MainActivity.this, Second_Activity.class);
                        startActivity(intent1);

                }
            }

            private void defaultClick() {
                // TODO Auto-generated method stub
                selectFragment(0);
            }

            private void selectFragment(int index) {
//        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
//        switch (index){
//            case 0:
//                /*cloudFragment=new CloudFragment();
//			    fragmentTransaction.add(R.id.fl, tuiJianFragment);*/
//                if (cloudFragment==null) {
//                cloudFragment=new CloudFragment();
//                 }
//                fragmentTransaction.replace(R.id.fl, cloudFragment);
//                break;
//            case 1:
//                if (headFragment==null) {
//                headFragment=new HeadFragment();
//                  }
//                fragmentTransaction.replace(R.id.fl, headFragment);
//                break;
//            case 2:if (znzFragment==null) {
//                znzFragment=new ZnzFragment();
//            }
//                fragmentTransaction.replace(R.id.fl, znzFragment);
//                break;
//                default: break;
//        }
        fragmentTransaction.commit();
        mViewPager.setCurrentItem(index);
        cloud.setTextColor(Color.GRAY);
        head.setTextColor(Color.GRAY);
        znz.setTextColor(Color.GRAY);


        }
    }